package solution.tree;

import common.TreeNode;
import common.util.TreeNodeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : wenchao.long
 * @date : Created in 2021/7/13 10:50 周二
 * @description : 二叉树的所有路径
 */
public class BinaryTreePathsSolution {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> paths = new ArrayList<>();
        bfs(paths, root, "");
        return paths;
    }

    private void bfs(List<String> paths, TreeNode root, String path) {
        if (root == null) {
            return;
        }
        StringBuilder res = new StringBuilder(path);
        res.append(root.val);
        if (root.left == null && root.right == null) {
            paths.add(res.toString());
        } else {
            res.append("->");
            bfs(paths, root.left, res.toString());
            bfs(paths, root.right, res.toString());
        }
    }

    public static void main(String[] args) {
        TreeNode root = TreeNodeUtil.create(1, 2, 3, null, 5);
        TreeNodeUtil.levelOrderList(root);

        List<String> strings = new BinaryTreePathsSolution().binaryTreePaths(root);
        System.out.println(strings);
    }


}
